﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AccManagerKw.Common;
using CustomControlz.Validations;
using Model;
using Model.Interfaces;
using Utilities;

namespace AccManagerKw.Masters
{
    public partial class FrmCountryMaster : FrmBaseMaster, ICrud
    {
        private ClsCommonFn _oCommFn = new ClsCommonFn();
        private CountryMaster _row = new CountryMaster();
        public FrmCountryMaster()
        {
            InitializeComponent();
        }
        private void FrmCountryMaster_Shown(object sender, EventArgs e)
        {
            _oCommFn.GridSetting(gvCountries, gcCountries);
            _oCommFn.GroupControl_Setting(gclMain);
            _oCommFn.GroupControl_Setting(gclOpen);
        }


        public bool IsValid()
        {
            ccDxValidationProvider1.Validate();
            return ccDxValidationProvider1.GetInvalidControls().Count <= 0;
        }
        public void ResetData()
        {
            _oCommFn.ClearFields(this);
            _oCommFn.DisableControls(gclMain, false);
            _row = new CountryMaster();
            gclMain.BringToFront();
            SetMode(ClsGlobalVarz.Mode.Add);
        }


        public bool OperationSave()
        {
            try
            {
                if (IsValid())
                {
                    if (Mode == ClsGlobalVarz.Mode.Add && _oCommFn.ValidateCrud(Mode))
                    {
                        SaveCountry();
                        _oCommFn.MsgBoxCrud(Mode);
                        return true;
                    }
                    else if (Mode == ClsGlobalVarz.Mode.Edit && _oCommFn.ValidateCrud(Mode))
                    {
                        SaveCountry();
                        _oCommFn.MsgBoxCrud(Mode);
                        return true;
                    }
                    else if (Mode != ClsGlobalVarz.Mode.Add && Mode != ClsGlobalVarz.Mode.Edit)
                    {
                        _oCommFn.WarningMsg("Check Mode");
                        return false;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
                return false;
            }
        }
        private void SaveCountry()
        {
            using (var context = new AccManagerEntities())
            {
                var oCm = new CountryMaster();
                if (Mode == ClsGlobalVarz.Mode.Add)
                {
                    oCm.CountryId = context.CountryMasters.ToList().Count > 0
                        ? context.CountryMasters.Max(x => x.CountryId) + 1
                        : 1;
                }
                else if (Mode == ClsGlobalVarz.Mode.Edit)
                    oCm = context.CountryMasters.Single(x => x.CountryId == _row.CountryId);
                oCm.CountryCode = txtCountryCode.Text;
                oCm.CountryName = txtCountryName.Text;
                if (Mode == ClsGlobalVarz.Mode.Add)
                    context.CountryMasters.Add(oCm);
                context.SaveChanges();
            }
        }


        public void OperationOpen()
        {
            try
            {
                gclOpen.BringToFront();
                var context = new AccManagerEntities();
                gcCountries.DataSource = context.CountryMasters.ToList();
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
            }
        }
        private void gvCountries_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                _row = (CountryMaster)gvCountries.GetFocusedRow();
                txtCountryCode.Text = _row.CountryCode;
                txtCountryName.Text = _row.CountryName;
                gclMain.BringToFront();
                _oCommFn.DisableControls(gclMain, true);
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
            }
        }


        public void OperationEdit()
        {
            if (_oCommFn.ValidateOpenRecord(_row.CountryId))
            {
                _oCommFn.DisableControls(gclMain, false);
                SetMode(ClsGlobalVarz.Mode.Edit);
            }
        }
        public bool OperationDelete()
        {
            try
            {
                if (_oCommFn.ValidateOpenRecord(_row.CountryId))
                {
                    SetMode(ClsGlobalVarz.Mode.Delete);
                    if (_oCommFn.ValidateCrud(Mode))
                    {
                        using (var context = new AccManagerEntities())
                        {
                            var oCm = context.CountryMasters.Single(x => x.CountryId == _row.CountryId);
                            context.CountryMasters.Remove(oCm);
                            context.SaveChanges();
                            _oCommFn.MsgBoxCrud(Mode);
                        }
                        return true;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                _oCommFn.TryCatchErrorMsg(ex.Message);
                return false;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }

    }
}
